﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL;
using Model;

namespace DAL
{
    public class Interim:IInterim
    {
        #region 静态变量部分
        private const string PARM_TEST_PRODUCTSN = "@ProductSN";
        private const string PARM_TEST_USERID = "@UserID";
        private const string PARM_TEST_STARTTIME = "@starttime";
        private const string PARM_TEST_STOPTIME = "@stoptime";
        private const string PARM_TEST_PACKAGEPRODUCTSN = "@PackageProductSN";
        #endregion

        #region 以下为过渡期，检索调试岗位相关数据
        //--------------------------------------------------------------------------------------------------------------------------
        public IList<TestInfo> GetDebugTestsbyProductSN(string productSN)
        {
            IList<TestInfo> tests = new List<TestInfo>();
            SqlParameter parm = new SqlParameter(PARM_TEST_PRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = productSN;
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Test_selectDebug_ByProductSN", parm))
            {
                while (rdr.Read())
                {
                    TestInfo test = new TestInfo(rdr.GetGuid(0), rdr.GetValue(1).ToString(), rdr.GetDateTime(2), rdr.GetValue(3).ToString()
                        , rdr.GetValue(4).ToString(), rdr.GetValue(5).ToString(), rdr.GetValue(6).ToString(), rdr.GetValue(7).ToString(), rdr.GetValue(8).ToString());
                    tests.Add(test);
                }
            }
            return tests;
        }

        public IList<TestInfo> GetDebugTestsbyPackageProductSN(string packageProductSN)
        {
            IList<TestInfo> tests = new List<TestInfo>();
            SqlParameter parm = new SqlParameter(PARM_TEST_PACKAGEPRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = packageProductSN;
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Test_selectDebug_ByPackageProductSN", parm))
            {
                while (rdr.Read())
                {
                    TestInfo test = new TestInfo(rdr.GetGuid(0), rdr.GetValue(1).ToString(), rdr.GetDateTime(2), rdr.GetValue(3).ToString()
                        , rdr.GetValue(4).ToString(), rdr.GetValue(5).ToString(), rdr.GetValue(6).ToString(), rdr.GetValue(7).ToString(), rdr.GetValue(8).ToString());
                    tests.Add(test);
                }
            }
            return tests;
        }


        public IList<TestInfo> GetDebugTestsbyDT(DateTime starttime, DateTime stoptime)
        {

            IList<TestInfo> tests = new List<TestInfo>();

            SqlParameter[] testParms = GetDTParameters();
            //Bind the parameter
            testParms[0].Value = starttime;
            testParms[1].Value = stoptime;

            //Execute a query to read the categories
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Test_selectDebug_DT", testParms))
            {
                while (rdr.Read())
                {
                    TestInfo test = new TestInfo(rdr.GetGuid(0), rdr.GetValue(1).ToString(), rdr.GetDateTime(2), rdr.GetValue(3).ToString()
                        , rdr.GetValue(4).ToString(), rdr.GetValue(5).ToString(), rdr.GetValue(6).ToString(), rdr.GetValue(7).ToString(), rdr.GetValue(8).ToString());
                    tests.Add(test);
                }
            }
            return tests;
        }

        public IList<TestInfo> GetDebugTestsbyUIDDT(string userID, DateTime starttime, DateTime stoptime)
        {

            IList<TestInfo> tests = new List<TestInfo>();

            SqlParameter[] testParms = GetUIDDTParameters();
            //Bind the parameter
            testParms[0].Value = userID;
            testParms[1].Value = starttime;
            testParms[2].Value = stoptime;

            //Execute a query to read the categories
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Test_selectDebug_UIDDT", testParms))
            {
                while (rdr.Read())
                {
                    TestInfo test = new TestInfo(rdr.GetGuid(0), rdr.GetValue(1).ToString(), rdr.GetDateTime(2), rdr.GetValue(3).ToString()
                        , rdr.GetValue(4).ToString(), rdr.GetValue(5).ToString(), rdr.GetValue(6).ToString(), rdr.GetValue(7).ToString(), rdr.GetValue(8).ToString());
                    tests.Add(test);
                }
            }
            return tests;
        }

        public int GetQbyUIDDT(string userID, DateTime starttime, DateTime stoptime)
        {
            SqlParameter[] testParms = GetUIDDTParameters();
            //Bind the parameter
            testParms[0].Value = userID;
            testParms[1].Value = starttime;
            testParms[2].Value = stoptime;

            int n = Convert.ToInt32(sqlHelper.ExecuteScalar(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Test_CountDebug_UIDDT", testParms));
            return n;
        }
        #endregion

        #region 参数部分
        private static SqlParameter[] GetDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_TEST_STARTTIME, SqlDbType.DateTime, 8),
					new SqlParameter(PARM_TEST_STOPTIME, SqlDbType.DateTime, 8)                
                };
            return parms;
        }

        private static SqlParameter[] GetUIDDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_TEST_USERID,SqlDbType.NVarChar,20),
					new SqlParameter(PARM_TEST_STARTTIME, SqlDbType.DateTime, 8),
					new SqlParameter(PARM_TEST_STOPTIME, SqlDbType.DateTime, 8)                
                };
            return parms;
        }
        #endregion


        //-------------------------------------------------------------------------------------------------------------------
        //以下为过渡期，统计调试岗位相关数据
        //按时间段统计各矢网、各测试员最终通过率
        protected IList<Time_PRInfo> GetDebug_OPRbyDT(string spname, DateTime starttime, DateTime stoptime)
        {
            IList<Time_PRInfo> t_qs = new List<Time_PRInfo>();
            SqlParameter[] testParms = GetDTParameters();
            testParms[0].Value = starttime;
            testParms[1].Value = stoptime;
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, spname, testParms))
            {
                while (rdr.Read())
                {
                    Time_PRInfo t_q = new Time_PRInfo(rdr.GetValue(0).ToString(), rdr.GetDecimal(1), rdr.GetInt32(2), rdr.GetInt32(3));
                    t_qs.Add(t_q);
                }
            }
            return t_qs;
        }

        public IList<Time_PRInfo> GetDebug_PRbyUID_DT(DateTime starttime, DateTime stoptime)
        {
            return GetDebug_OPRbyDT("Test_Debug_PRbyUID_DT", starttime, stoptime);
        }

        public IList<Time_PRInfo> GetDebug_PRbyEID_DT(DateTime starttime, DateTime stoptime)
        {
            return GetDebug_OPRbyDT("Test_Debug_PRbyEID_DT", starttime, stoptime);
        }
    }
}
